package me.zhengjie.spider.util.pipeline;

import lombok.Data;
import me.zhengjie.spider.comic.domain.SpiderComicContent;
import me.zhengjie.spider.comic.domain.SpiderComicDetail;
import me.zhengjie.spider.comic.service.SpiderComicContentService;
import me.zhengjie.spider.comic.service.SpiderComicDetailService;
import me.zhengjie.spider.common.SpiderAttributeBean;
import me.zhengjie.spider.common.SpiderConstant;
import me.zhengjie.spider.domain.comic.ComicBean;
import me.zhengjie.spider.domain.comic.ComicRuleBean;
import me.zhengjie.spider.manager.SpiderAsyncManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.ArrayList;
import java.util.List;

@Data
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class SpiderComicContentPipeline implements Pipeline {
    private ComicBean bean;
    private ComicRuleBean ruleBean;
    private SpiderAttributeBean attributeBean;

    @Autowired
    private SpiderComicDetailService spiderComicDetailService;
    @Autowired
    private SpiderComicContentService spiderComicContentService;
    @Autowired
    private SpiderAsyncManager spiderAsyncManager;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void process(ResultItems resultItems, Task task) {
        List<String> imgList = resultItems.get(SpiderConstant.IMG_LIST);
        String nextPage = resultItems.get(SpiderConstant.NEXT_PAGE);
        List<SpiderComicContent> spiderContents = new ArrayList<>();
        this.bean.setNextPage(nextPage);
        int num = this.spiderComicContentService.countByComicDetailId(this.bean.getComicDetailId());
        if (StringUtils.isEmpty(nextPage) && num > 0) {
            return;
        }
        SpiderComicDetail spiderComicDetail = new SpiderComicDetail();
        BeanUtils.copyProperties(this.bean, spiderComicDetail);
        this.spiderComicDetailService.update(spiderComicDetail);
        for (int i = 0; i < imgList.size(); i++) {
            SpiderComicContent spiderComicContent = new SpiderComicContent();
            spiderComicContent.setComicDetailId(spiderComicDetail.getComicDetailId());
            spiderComicContent.setImg(imgList.get(i));
            spiderComicContent.setSortNum(i);
            spiderContents.add(spiderComicContent);
        }
        this.spiderComicContentService.createBatch(spiderContents);
        if (StringUtils.isNotEmpty(nextPage)) {
            this.spiderNextPage(this.bean, nextPage);
        }
    }

    public void spiderNextPage(ComicBean bean, String nextPage) {
        SpiderComicDetail nextSpiderComicDetail = new SpiderComicDetail();
        nextSpiderComicDetail.setComicId(bean.getComicId());
        nextSpiderComicDetail.setAlt(String.valueOf(Integer.parseInt(bean.getAlt()) + 1));
        nextSpiderComicDetail.setHref(nextPage);
        nextSpiderComicDetail.setDeletedFlag(false);
        nextSpiderComicDetail = this.spiderComicDetailService.create(nextSpiderComicDetail);
        ComicBean comicBean = new ComicBean();
        BeanUtils.copyProperties(nextSpiderComicDetail, comicBean);
        this.spiderAsyncManager.spiderComicContent(comicBean, this.ruleBean, this.attributeBean);
    }
}
